<?php
class StudentinfosController extends AppController {

	var $name = 'Studentinfos';
	var $helpers = array('Html', 'Form');
	
	var $uses = array('Studentinfo');

/* 	function index() {
		$this->Studentinfo->recursive = 0;
		$this->set('studentinfos', $this->paginate());
	} */

/* 	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Studentinfo', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('studentinfo', $this->Studentinfo->read(null, $id));
	} */

/* 	function add() {
		if (!empty($this->data)) {
			$this->Studentinfo->create();
			if ($this->Studentinfo->save($this->data)) {
		
				 // тут обрабатываем целевое направление добавляем если есть
				
				$studentinfo_id = $this->Studentinfo->id;
				$targetingintake_region_id = $this->data['Studentinfo']['targetingintake_region_id'];
				// если записи небыло и id региона а ее не было так как создаем новую запись
				if ($targetingintake_region_id) {
					$this->loadModel('Targetingintake');					
					$newdata = array('Targetingintake' => array ('studentinfo_id' => $studentinfo_id, 'region_id' => $targetingintake_region_id, 'info' => 'изменено в ручную'));
					$this->Targetingintake->create();
					$this->Targetingintake->save($newdata);
					$this->Session->setFlash(__('Запись о целевом направлении добавлена!', true));
				}				
				$this->Session->setFlash(__('Информация о студенте была добавлена!', true));
				$this->redirect(array('action' => 'added'));
			} else {
				$this->Session->setFlash(__('Информация о студенте НЕ была добавлена! Попробуйте снова.', true));
			}
		}
		$privileges = $this->Studentinfo->Privilege->find('list');
		$genders = $this->Studentinfo->Gender->find('list');
		$citizenships = $this->Studentinfo->Citizenship->find('list');
		$regions = $this->Studentinfo->Region->find('list');
		$educationdoctypes = $this->Studentinfo->Educationdoctype->find('list');
		$foreignlanguages = $this->Studentinfo->Foreignlanguage->find('list');
		
		$this->set(compact('privileges','genders','citizenships','regions','educationdoctypes','foreignlanguages'));
		
		if ($this->params['named']) {
			$student_id = $this->params['named']['student_id'];
		}
		else {
			$student_id = $this->data['Studentinfo']['student_id'];
		}
		
		$student = $this->Studentinfo->Student->read(null, $student_id); 
		$this->set('student', $student);
		$studentgroup_id = $student['Student']['studentgroup_id'] ;
		// курсы 
		$this->loadModel('Course');
		$courses = $this->Course->find('list');	
		$this->set(compact('facultets', 'professions', 'courses'));
		
		// группа
		$this->loadModel('Studentgroup');		
		$studentgroup = $this->Studentgroup->find('all', array('conditions' => array('Studentgroup.id' => $studentgroup_id), 'recursive' => 0,));
		$this->set(compact('studentgroup'));
		
		// специальность
		$this->loadModel('Profession');
		$profession = $this->Profession->find('all', array('conditions' => array('Profession.id' => $studentgroup[0]['Course']['profession_id']), 'recursive' => 0));
		$this->set(compact('profession'));
		// форма и тип обучения
		$this->loadModel('Learningform');
		$learningforms = $this->Learningform->find('list');
		$this->loadModel('Learningtype');
		$this->set('learningforms', $learningforms);
		$learningtypes = $this->Learningtype->find('list');
		$this->set('learningtypes', $learningtypes);
		
		// подключаем собственный шаблон		
		$this->render('/','defaultshowinfo','add');	
		// $this->render('/','blank','add');		
	} */

/* 	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Studentinfo', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Studentinfo->save($this->data)) {
			
				 // тут обрабатываем целевое направление добавляем изменяем удаляем
	
				$studentinfo_id = $this->data['Studentinfo']['id'];
				$targetingintake_region_id = $this->data['Studentinfo']['targetingintake_region_id'];
				$targetingintake_info = $this->data['Studentinfo']['targetingintake_info'];
				// обработать целевой набор
				$this->loadModel('Targetingintake');
				$count = $this->Targetingintake->find('count', array('conditions' => array('Targetingintake.studentinfo_id' => $studentinfo_id)));
				// если запись была а теперь id региона 0 то удаляем
				if (!$targetingintake_region_id && $count) {
					$targetingintake = $this->Targetingintake->find('all', array('conditions' => array('Targetingintake.studentinfo_id' => $studentinfo_id)));
					$targetingintake_id = $targetingintake[0]['Targetingintake']['id'];
					$this->Targetingintake->del($targetingintake_id);
					$this->Session->setFlash(__('Запись о целевом направлении удалена!', true));
				}
				// если запись была но ее надо изменить
				if ($targetingintake_region_id && $count){
					$targetingintake = $this->Targetingintake->find('all', array('conditions' => array('Targetingintake.studentinfo_id' => $studentinfo_id)));
					$targetingintake_id = $targetingintake[0]['Targetingintake']['id'];
					$this->Targetingintake->id = $targetingintake_id;
					$newdata = array('Targetingintake' => array ('studentinfo_id' => $studentinfo_id, 'region_id' => $targetingintake_region_id, 'info' => $targetingintake_info));
					$this->Targetingintake->save($newdata);
					$this->Session->setFlash(__('Запись о целевом направлении обновлена!', true));
				}
				// если записи небыло и id региона не 0
				if ($targetingintake_region_id && !$count) {
					$newdata = array('Targetingintake' => array ('studentinfo_id' => $studentinfo_id, 'region_id' => $targetingintake_region_id, 'info' => $targetingintake_info));
					$this->Targetingintake->create();
					$this->Targetingintake->save($newdata);
					$this->Session->setFlash(__('Запись о целевом направлении добавлена!', true));
				}
				$this->Session->setFlash(__('Данные студента были сохранены', true));
				$this->redirect(array('action' => 'added'));
			} else {
				$this->Session->setFlash(__('The Studentinfo could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Studentinfo->read(null, $id);
		}
		$privileges = $this->Studentinfo->Privilege->find('list');
		$genders = $this->Studentinfo->Gender->find('list');
		$citizenships = $this->Studentinfo->Citizenship->find('list');
		$regions = $this->Studentinfo->Region->find('list');
		$educationdoctypes = $this->Studentinfo->Educationdoctype->find('list');
		$foreignlanguages = $this->Studentinfo->Foreignlanguage->find('list');
		$this->set(compact('privileges','genders','citizenships','regions','educationdoctypes','foreignlanguages'));
		

		$studentgroup_id = $this->data['Student']['studentgroup_id'] ;
		// курсы 
		$this->loadModel('Course');
		$courses = $this->Course->find('list');	
		$this->set(compact('facultets', 'professions', 'courses'));
		
		// группа
		$this->loadModel('Studentgroup');		
		$studentgroup = $this->Studentgroup->find('all', array('conditions' => array('Studentgroup.id' => $studentgroup_id), 'recursive' => 0,));
		$this->set(compact('studentgroup'));
		
		// специальность
		$this->loadModel('Profession');
		$profession = $this->Profession->find('all', array('conditions' => array('Profession.id' => $studentgroup[0]['Course']['profession_id']), 'recursive' => 0));
		$this->set(compact('profession'));
		// форма и тип обучения
		$this->loadModel('Learningform');
		$learningforms = $this->Learningform->find('list');
		$this->loadModel('Learningtype');
		$this->set('learningforms', $learningforms);
		$learningtypes = $this->Learningtype->find('list');
		$this->set('learningtypes', $learningtypes);
		
		// типы родственников
		$this->LoadModel('Relativestype');
		$relativestypes = $this->Relativestype->find('list');
		$this->set(compact('relativestypes'));
		
		// типы статуса супруга
		$this->loadModel('Matestatus');
		$matestatuses = $this->Matestatus->find('list');
		$this->set(compact('matestatuses'));
		
		// выводим целевое направление
		$studentinfo_id = $this->data['Studentinfo']['id'] ;
		$this->set('studentinfo_id', $studentinfo_id);
		$this->loadModel('Targetingintake');
		$targetingintake = $this->Targetingintake->find('all', array('conditions' => array('Targetingintake.studentinfo_id' => $studentinfo_id), 'recursive' => -1));	
		$this->set('targetingintake', $targetingintake);

		// подключаем собственный шаблон		
		 $this->render('/','defaultshowinfo','edit');			
	} */

/* 	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Studentinfo', true));
			$this->redirect(array('action' => 'index'));
		}
		if ($this->Studentinfo->del($id)) {
			$this->Session->setFlash(__('Studentinfo deleted', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('The Studentinfo could not be deleted. Please, try again.', true));
		$this->redirect(array('action' => 'index'));
	} */
	
/*
 * метод для вывода изображения из таблицы в html страницу
 */	
	function showimage($id) {
		Configure::write('debug', 0);
		$this->loadModel('Studentfoto');
		$this->Studentfoto->recursive=-1;
        $photofile = $this->Studentfoto->find('all', array('conditions' => array('Studentfoto.student_id' => $id), 'fields' => array('Studentfoto.foto')));
        if (!isset($photofile)){
        	$photofile = 'НЕТ ИЗОБРАЖЕНИЯ';
        }
        $this->set('photofile', $photofile);
        $this->render('/','showimagefile','showimage');
	}
/*
 * метод для отображения информации по студенту
 */
	function showinfo ($id) {
		$student = $this->Studentinfo->find('all', array('conditions' => array('Studentinfo.student_id' => $id)));
		$this->set(compact('student'));
		$this->loadModel('Gender');
		$genders = $this->Gender->find('list');
		$this->set(compact('genders'));
		$this->loadModel('Citizenship');
		$citizenships = $this->Citizenship->find('list');
		$this->set(compact('citizenships'));	
		$this->loadModel('Region');
		$regions = $this->Region->find('list');
		$this->set(compact('regions'));			
		$this->loadModel('Educationdoctype');
		$educationdoctypes = $this->Educationdoctype->find('list');
		$this->set(compact('educationdoctypes'));
		$this->loadModel('Foreignlanguage');
		$foreignlanguages = $this->Foreignlanguage->find('list');
		$this->set(compact('foreignlanguages'));
		$this->loadModel('Ordertype');
		$listordertypes = $this->Ordertype->find('list');
		$this->set(compact('listordertypes'));
		$this->loadModel('Order');
		
		// грузим приказы по студенту
		$orders = $this->Order->find('all', array('conditions' => array('Order.student_id' => $id), 'order' => 'Order.id'));
		$this->set('orders', $orders);
		// целевое направление
		$studentinfo_id = $student[0]['Student']['id'];
		$this->loadModel('Targetingintake');
		$targetingintake = $this->Targetingintake->find('all', array('conditions' => array('Targetingintake.studentinfo_id' => $studentinfo_id), 'recursive' => -1));	
		$this->set('targetingintake', $targetingintake);		
		// курсы 
		$this->loadModel('Course');
		$courses = $this->Course->find('list');	
		$this->set(compact('facultets', 'professions', 'courses'));
		
		// группа
		$this->loadModel('Studentgroup');		
		$studentgroup = $this->Studentgroup->find('all', array('conditions' => array('Studentgroup.id' => $student[0]['Student']['studentgroup_id']), 'recursive' => 0,));
		$this->set(compact('studentgroup'));
		// специальность
		$this->loadModel('Profession');
		$profession = $this->Profession->find('all', array('conditions' => array('Profession.id' => $studentgroup[0]['Course']['profession_id']), 'recursive' => 0));
		$this->set(compact('profession'));
		// форма и тип обучения
		$this->loadModel('Learningform');
		$learningforms = $this->Learningform->find('list');
		$this->loadModel('Learningtype');
		$this->set('learningforms', $learningforms);
		$learningtypes = $this->Learningtype->find('list');
		$this->set('learningtypes', $learningtypes);
		
		// типы родственников
		$this->LoadModel('Relativestype');
		$relativestypes = $this->Relativestype->find('list');
		$this->set(compact('relativestypes'));
		
		// типы статуса супруга
		$this->loadModel('Matestatus');
		$matestatuses = $this->Matestatus->find('list');
		$this->set(compact('matestatuses'));
				
		// подключаем собственный шаблон
		// $this->render('/','defaultshowinfo','showinfo');	
	}
	
	function searchedshowinfo ($id) {
		$student = $this->Studentinfo->find('all', array('conditions' => array('Studentinfo.student_id' => $id)));
		$this->set(compact('student'));
		$this->loadModel('Gender');
		$genders = $this->Gender->find('list');
		$this->set(compact('genders'));
		$this->loadModel('Citizenship');
		$citizenships = $this->Citizenship->find('list');
		$this->set(compact('citizenships'));
		$this->loadModel('Region');
		$regions = $this->Region->find('list');
		$this->set(compact('regions'));
		$this->loadModel('Educationdoctype');
		$educationdoctypes = $this->Educationdoctype->find('list');
		$this->set(compact('educationdoctypes'));
		$this->loadModel('Foreignlanguage');
		$foreignlanguages = $this->Foreignlanguage->find('list');
		$this->set(compact('foreignlanguages'));
		$this->loadModel('Ordertype');
		$listordertypes = $this->Ordertype->find('list');
		$this->set(compact('listordertypes'));
		$this->loadModel('Order');
	
		// грузим приказы по студенту
		$orders = $this->Order->find('all', array('conditions' => array('Order.student_id' => $id), 'order' => 'Order.id'));
		$this->set('orders', $orders);
		// целевое направление
		$studentinfo_id = $student[0]['Student']['id'];
		$this->loadModel('Targetingintake');
		$targetingintake = $this->Targetingintake->find('all', array('conditions' => array('Targetingintake.studentinfo_id' => $studentinfo_id), 'recursive' => -1));
		$this->set('targetingintake', $targetingintake);
		// курсы
		$this->loadModel('Course');
		$courses = $this->Course->find('list');
		$this->set(compact('facultets', 'professions', 'courses'));
	
		// группа
		$this->loadModel('Studentgroup');
		$studentgroup = $this->Studentgroup->find('all', array('conditions' => array('Studentgroup.id' => $student[0]['Student']['studentgroup_id']), 'recursive' => 0,));
		$this->set(compact('studentgroup'));
		// специальность
		$this->loadModel('Profession');
		$profession = $this->Profession->find('all', array('conditions' => array('Profession.id' => $studentgroup[0]['Course']['profession_id']), 'recursive' => 0));
		$this->set(compact('profession'));
		// форма и тип обучения
		$this->loadModel('Learningform');
		$learningforms = $this->Learningform->find('list');
		$this->loadModel('Learningtype');
		$this->set('learningforms', $learningforms);
		$learningtypes = $this->Learningtype->find('list');
		$this->set('learningtypes', $learningtypes);
	
		// типы родственников
		$this->LoadModel('Relativestype');
		$relativestypes = $this->Relativestype->find('list');
		$this->set(compact('relativestypes'));
	
		// типы статуса супруга
		$this->loadModel('Matestatus');
		$matestatuses = $this->Matestatus->find('list');
		$this->set(compact('matestatuses'));
	
		// подключаем собственный шаблон
		 $this->render('/','default','showinfo');
	}	
/* 	function added() {
		
	} */
}
?>